package com.animee.day11.utils;

import com.animee.day11.ResourceTable;
import com.animee.day11.bean.FilmInfoBean;
import com.bumptech.glide.Glide;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.agp.components.element.PixelMapElement;
import ohos.global.resource.NotExistException;

import java.io.IOException;
import java.util.List;

public class FilmInfoProvider extends BaseItemProvider {
    AbilitySlice context;
    List<FilmInfoBean> mDatas;
    public FilmInfoProvider(AbilitySlice context, List<FilmInfoBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }
    @Override
    public int getCount() {
        return mDatas.size();
    }
    @Override
    public Object getItem(int i) {
        return mDatas.get(i);
    }
    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        if (component == null) {
            component = LayoutScatter.getInstance(context)
                    .parse(ResourceTable.Layout_item_film,componentContainer,false);
        }
        Text titleTv = component.findComponentById(ResourceTable.Id_item_fm_tv_title);
        Text actorTv = component.findComponentById(ResourceTable.Id_item_fm_tv_actor);
        Text sglTv = component.findComponentById(ResourceTable.Id_item_fm_tv_sgl);
        Text numTv = component.findComponentById(ResourceTable.Id_item_fm_tv_num);
        Text scoreTv = component.findComponentById(ResourceTable.Id_item_fm_tv_score);
        Text pnumTv = component.findComponentById(ResourceTable.Id_item_fm_tv_pnum);
        Rating rating = component.findComponentById(ResourceTable.Id_item_fm_rat);
        Image iv = component.findComponentById(ResourceTable.Id_item_fm_iv);

        int num = i+1;
        FilmInfoBean bean = mDatas.get(i);
        titleTv.setText(bean.getTitle());
        numTv.setText(num+"");
        pnumTv.setText(bean.getVote_count()+"人评价");
        scoreTv.setText(bean.getScore());
        //显示演员信息
        String actors = "";
        for (int j = 0; j < bean.getActors().size(); j++) {
            String ac = bean.getActors().get(j);
            actors += (ac+"/");
        }
        actorTv.setText(actors);
        //显示上映事件1994-06-17   国家    类型
        String sgl = "";
//        int lineIndex = bean.getRelease_date().indexOf("-");
        String time = bean.getRelease_date().substring(0,4);
        sgl = time+"/";
        for (int j = 0; j < bean.getRegions().size(); j++) {
            String reg = bean.getRegions().get(j);
            sgl += (reg+"/");
        }

        for (int j = 0; j < bean.getTypes().size(); j++) {
            String type = bean.getTypes().get(j);
            sgl += (type+"/");
        }
        sglTv.setText(sgl);
//  加载网络图片的方法
        String coverUrl = bean.getCover_url();
        if (coverUrl!=null &&!coverUrl.isEmpty()){
            Glide.with(context).load(coverUrl).placeholder(ResourceTable.Media_load_failed)
                            .error(ResourceTable.Media_load_failed).into(iv);
//            Glide.with(context).load(coverUrl).into(iv);
        }else{
            iv.setPixelMap(ResourceTable.Media_load_failed);
        }
//   设置评分(这有个坑，以后改)
        rating.setIsOperable(true);   //设置不能被滑动
        float ss = (Float.valueOf( bean.getScore()))/2;
        rating.setScore(ss);

        try {
            rating.setFilledElement(
                    new PixelMapElement(context.getResourceManager()
                            .getResource(ResourceTable.Media_ic_one_f)));
            rating.setUnfilledElement(
                    new PixelMapElement(context.getResourceManager()
                                    .getResource(ResourceTable.Media_ic_one)));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return component;
    }
}